/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ItemView.java
 *
 * Created on 23-Mar-2011, 11:18:53 AM
 */

package pos.ui.screen.administrator;

import pos.control.DeleteItemUPCControl;
import pos.control.ItemViewControl;
import pos.storage.db.mysql.SelectQueryTableModel;
import pos.storage.db.sql.SQL;
import pos.ui.screen.Payment;

/**
 *
 * @author rsrikanth
 */
public class ItemView extends javax.swing.JPanel {

    String nodeTitle = "Items View";
    SelectQueryTableModel sTableModel = new SelectQueryTableModel();
    /** Creates new form ItemView */
    public ItemView() {
        initComponents();
        ItemViewControl adminControl = new ItemViewControl();
        adminControl.genTable(sTableModel, SQL.DISPALY_TABLE); //null will perform select *
    }

    /** This method is called from within the constructor to
     * initialise the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        addItem = new javax.swing.JButton();
        modifyItem = new javax.swing.JButton();
        deleteItem = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(800, 600));
        setLayout(new java.awt.BorderLayout());

        jTable1.setModel(sTableModel);
        jScrollPane1.setViewportView(jTable1);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

        addItem.setText("Add Item");
        addItem.setPreferredSize(new java.awt.Dimension(90, 23));
        addItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addItemActionPerformed(evt);
            }
        });
        jPanel1.add(addItem);

        modifyItem.setText("Modify Item");
        modifyItem.setPreferredSize(new java.awt.Dimension(100, 23));
        modifyItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyItemActionPerformed(evt);
            }
        });
        jPanel1.add(modifyItem);

        deleteItem.setText("Delete Item");
        deleteItem.setPreferredSize(new java.awt.Dimension(100, 23));
        deleteItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteItemActionPerformed(evt);
            }
        });
        jPanel1.add(deleteItem);

        add(jPanel1, java.awt.BorderLayout.PAGE_START);
    }// </editor-fold>//GEN-END:initComponents

    private void addItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addItemActionPerformed
        // TODO add your handling code here:
        //(new Test1()).setVisible(true);
        AddItemUPCPanel thePanel = new AddItemUPCPanel();
        thePanel.setVisible(true);
        thePanel.setItemView(this);
    }//GEN-LAST:event_addItemActionPerformed

    private void deleteItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteItemActionPerformed
        // TODO add your handling code here:
        DeleteItemUPCControl deletecontrol = new DeleteItemUPCControl();
        int rows[] = jTable1.getSelectedRows();
        String s = "";
        for(int j=0; j<rows.length; j++){
            s = String.valueOf(sTableModel.getValueAt(rows[j],0));
            deletecontrol.deleteItem(s);
        }
        ItemViewControl adminControl = new ItemViewControl();
        sTableModel.removeAll();
        adminControl.genTable(sTableModel, SQL.DISPALY_TABLE);
                
    }//GEN-LAST:event_deleteItemActionPerformed

    private void modifyItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyItemActionPerformed
        // TODO add your handling code here:
        int rows[] = jTable1.getSelectedRows();
        ModifyItemUPCPanel thePanel = new ModifyItemUPCPanel();
        String s = "";
        if(rows.length > 0){
            s = String.valueOf(sTableModel.getValueAt(rows[0],0));
            thePanel.setVisible(true);
            thePanel.setSelectItemId(s);
            thePanel.setItemView(this);
        }
    }//GEN-LAST:event_modifyItemActionPerformed


    /**
     * 
     * @return
     */
    public String toString(){
        return nodeTitle;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addItem;
    private javax.swing.JButton deleteItem;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton modifyItem;
    // End of variables declaration//GEN-END:variables

}
